﻿using System;
using System.Windows;
using System.Threading;
using System.Collections.ObjectModel;

// Toolkit namespace
using SimpleMvvmToolkit;

namespace ToolRental.Client.SMvvm.Admin
{
    /// <summary>
    /// This class contains properties that a View can data bind to.
    /// <para>
    /// Use the <strong>mvvmprop</strong> snippet to add bindable properties to this ViewModel.
    /// </para>
    /// </summary>
    public class DeviceTypesViewModel : ViewModelBase<DeviceTypesViewModel>,INotifyError
    {

        private IDeviceTypeServiceAgent serviceAgent;

        #region Constructor
        public DeviceTypesViewModel() { }

        public    DeviceTypesViewModel(IDeviceTypeServiceAgent serviceAgent)
        {
            this.serviceAgent = serviceAgent;
            LoadDeviceTypes();
        }
        #endregion

        #region LoadDeviceTypes
        public async void LoadDeviceTypes()
        {
            var deviceTypes = await serviceAgent.GetDeviceTypeList(this);
            DeviceTypeList = new ObservableCollection<DeviceType>(deviceTypes);
        }
        #endregion

        #region DeviceTypeList
        private ObservableCollection<DeviceType> deviceTypeList;
        public ObservableCollection<DeviceType> DeviceTypeList
        {
            get { return deviceTypeList; }
            set
            {
                deviceTypeList = value;
                NotifyPropertyChanged(m => m.DeviceTypeList);
            }
        }
        #endregion

        #region AddNewDeviceType
        public void AddNewDeviceType()
        {
            if ((NewToolTypeBrand != "") && (NewToolTypeName != ""))
            {
                serviceAgent.AddDeviceType(NewToolTypeBrand, NewToolTypeName, this);
                NewToolTypeBrand = "";
                NewToolTypeName = "";
                LoadDeviceTypes();
            }
            else
            {
                ErrorOccured("Adja meg a szükséges adatokat! (Márka & Típus)", null);
            }

        }
        #endregion

        #region  NotifyError
        public event EventHandler<NotificationEventArgs<Exception>> ErrorNotice;

        private void NotifyError(string message, Exception error)
        {
            Notify(ErrorNotice, new NotificationEventArgs<Exception>(message, error));
        }
        #endregion

        #region ErrorOccured
        public void ErrorOccured(string message, Exception exception)
        {
            MessageBox.Show(message);
        }
        #endregion

        #region NewToolTypeBrand
        private string newToolTypeBrand;
        public string NewToolTypeBrand
        {
            get { return newToolTypeBrand; }
            set
            {
                newToolTypeBrand = value;
                NotifyPropertyChanged(m => m.NewToolTypeBrand);
            }
        }
        #endregion

        #region NewToolTypeName
        private string newToolTypeName;
        public string NewToolTypeName
        {
            get { return newToolTypeName; }
            set
            {
                newToolTypeName = value;
                NotifyPropertyChanged(m => m.NewToolTypeName);
            }
        }
        #endregion
    }
}